/**
 * Created by Delicate on 2017/5/31.
 */
let layer;
let ajax;
let WebUploader
require(["jquery","common","layer","WdatePicker","ajax","webuploader"],(a,b,c,d,e,f)=>{
    layer = c;
    WebUploader=f;
    init();
    monitor();
    service();
    ajax=e;
})

function deleteWrapper(id){
	$("#wrpper_"+id).remove();
	$("#source_"+id).remove();
}

function initUploader(){
	var srcStr;
    var uploader = WebUploader.create({
        auto: true,
        duplicate:true,
        swf:  contextPath+ '/assets/front/web/script/upload/Uploader.swf',
        server: contextPath+'/qiniu/upload',
        pick: {
            id:'#fileClick'
        },
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
        }
    });
    // 当有文件添加进来的时候
    uploader.on( 'fileQueued', function( file ) {
        var up=$("#uploaded");
        // 创建缩略图
        // thumbnailWidth x thumbnailHeight 为 100 x 100
        uploader.makeThumb( file, function( error, src ) {
            if ( error ) {
                $img.replaceWith('<span>不能预览</span>');
                return;
            }
            file_id =file.id;
            srcStr=src;
            up.append('<div class="u-row-cell-upload-btn" id="wrpper_'+file_id+'"><div id='+file_id+' class="u-row-cell-upload-btn-border" >正在上传</div></div>')
            var delLink = "<div style='text-align:center;'><a href='javascript:;' onclick=deleteWrapper('"+file_id+"') style='color:#c5c2c2'>删除</a></div>";
            var c=$("#"+file.id);
            c.after(delLink);
            c.css('height',"95%");
            var input=$("#hiddenFiles");
            input.append('<input type="hidden" name="source" id="source_'+file_id+'">');
        }, "100", "100" );
    });
    //文件上传成功处理
    uploader.on( 'uploadSuccess', function( file,resp ) {
        var c=$("#"+file.id);
        c.html("");
        c.css('background','url('+resp._raw+') center/contain no-repeat'); 
        var e=$("#source_"+file.id);
        e.val(resp._raw);
    });
    //文件上传失败处理
    uploader.on( 'uploadError', function( file ) {
    	layer.alert(file.name+"上传失败！！")
    });
    // 文件上传过程中创建进度条实时显示。
    uploader.on( 'uploadProgress', function( file, percentage ) {
    });

}



const init = ()=>{
    $("#extension_type").val($(".u-row-box-check-active").attr("code"));
    // $("#fileClick").click(function(){
    //     $("#files").trigger("click");
    // })
    initUploader();
}
const monitor = ()=>{
    $("#bxxs .u-row-box-check").on("click",function(){
        $("#bxxs .u-row-box-check-active").removeClass("u-row-box-check-active");
        $(this).addClass("u-row-box-check-active");
        $("#h5_type").val($(this).attr("code"));
        if($(this).attr('code') === '1'){
            $("#pic_panel").hide();
        }else{
            $("#pic_panel").show();
        }
    });
    $("#tgfs .u-row-box-check").on("click",function(){
        $("#tgfs .u-row-box-check-active").removeClass("u-row-box-check-active");
        $(this).addClass("u-row-box-check-active");
        var code = $(this).attr("code");
        $("#extension_type").val(code);
        if(code=="2"){
        	$("#wh_number").attr("readonly",true);
        	$("#wh_number").val(0);
        	$("#next").html("发布任务");
        }else{
        	$("#wh_number").attr("readonly",false);
        	$("#wh_number").val("");
        	$(".task-next").html("下一步");
        }
    });
}
const service = ()=>{

}
var successCallBack=function(data){
	if(data.result){
		var id = data.data;
		window.location.href= contextPath+'/platform/task/dispatcher?id='+id;
    }else{
        layer.alert(data.msg)
    }
}

var asubmit=function(){
    const task_title = $("#task_title").val().trim();
    const web_url = $("#web_url").val().trim();
    const task_start_time = $("#task_start_time").val().trim();
    const task_end_time = $("#task_end_time").val().trim();
    const wh_number = $("#wh_number").val().trim();
    const task_description = $("#task_description").val().trim();
    if(!task_title|| task_title.length > 50){
        layer.alert("请输入1-50字任务标题");
        return;
    }
    
    if(!task_start_time){
        layer.alert("请选择任务开始时间");
        return;
    }
    if(!task_end_time){
        layer.alert("请选择任务结束时间");
        return;
    }
    if(!wh_number){
        layer.alert("请输入主播人数");
        return;
    }
    
    var reg = new RegExp("^[0-9]*$");  
    if(!reg.test(wh_number)){  
    	layer.alert("主播人数只能是数字");
    	return;
    }
    if(parseInt(wh_number)>10){
        layer.alert("当前推广方式为指定主播模式，只能设置最多10个主播");
        return;
    }
    
    if(!task_description){
        layer.alert("请输入任务说明");
        return;
    }
    if($("#bxxs .u-row-box-check-active").attr('code') == '2' && $("#uploaded").find('div').length < 1){
        layer.alert("请选择要上传的素材");
        return;
    }
    
    var data = $("#taskForm").serialize();
    var ajax = new BackendTool({"data":data,"method":"post","url":contextPath+"/platform/task/save"});
    ajax.successCallBack = successCallBack;
    ajax.ajaxRequest();
    
}

